// send_taobao_data_test.js
const fs = require('fs');
const path = require('path');
const http = require('http');

// 读取淘宝进货单数据文件
const taobaoDataFilePath = path.join(__dirname, '通过插件拦截响应数据（淘宝进货单）.md');
const taobaoData = fs.readFileSync(taobaoDataFilePath, 'utf8');

// 解析JSON数据
const jsonData = JSON.parse(taobaoData);

// 配置请求选项
const options = {
  hostname: '127.0.0.1',
  port: 9001,
  path: '/api/v1/plugin/data/receive',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
  }
};

console.log('正在向API端点发送淘宝采购订单数据...');
console.log('请求地址: http://127.0.0.1:9001/api/v1/plugin/data/receive');

// 创建请求
const req = http.request(options, (res) => {
  console.log(`响应状态码: ${res.statusCode}`);
  
  let responseData = '';
  
  res.on('data', (chunk) => {
    responseData += chunk;
  });
  
  res.on('end', () => {
    console.log('响应数据:');
    console.log(responseData);
    
    try {
      const result = JSON.parse(responseData);
      if (result.code === 200) {
        console.log('数据发送成功！');
        console.log('接收数量:', result.data.receivedCount);
        console.log('处理数量:', result.data.processedCount);
        console.log('批次ID:', result.data.batchId);
      } else {
        console.log('数据发送失败:', result.message);
      }
    } catch (e) {
      console.log('响应不是JSON格式:', responseData);
    }
  });
});

req.on('error', (error) => {
  console.error('请求出错:', error.message);
});

// 发送数据
req.write(JSON.stringify(jsonData));
req.end();

console.log('请求已发送，请等待响应...');